package com.qf.market.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.market.po.Order;
import com.qf.market.po.OrderDetail;
import com.qf.market.service.OrderDetailService;
import com.qf.market.service.OrderService;
import com.qf.market.utils.LayUIOperate;
import com.qf.market.utils.TableData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {
    int x=0;
    @Resource
    private OrderService orderService;
    @Resource
    private OrderDetailService orderDetailService;

    //初始遍历、条件查询...
    @RequestMapping("/getAllOrderList")
    @ResponseBody
    public TableData getAllOrderList(HttpServletRequest request){
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String name = request.getParameter("name");
        String orderNum = request.getParameter("orderNum");
        String start_time = request.getParameter("start_time");
        String end_time = request.getParameter("end_time");
        String lowMoney = request.getParameter("lowMoney");
        String highMoney = request.getParameter("highMoney");
        String status = request.getParameter("status");
        Map params = new HashMap();
        params.put("name", name);
        params.put("orderNum", orderNum);
        params.put("start_time", start_time);
        params.put("end_time", end_time);
        params.put("lowMoney", lowMoney);
        params.put("highMoney", highMoney);
        params.put("status", status);


        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<Order> data = orderService.getAllOrderByPage(params);
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(data.getTotal());//设置总条数
        tableData.setData(data.getList());//设置当前的数据
        System.out.println(params);
        return tableData;
    }


    @RequestMapping("/addOrder")
    @ResponseBody
    public LayUIOperate addOrder(@RequestBody JSONObject object){
        LayUIOperate operate=new LayUIOperate();
        String orderNum = object.getString("orderNum");
        String name = object.getString("name");
        String tel = object.getString("tel");
        String orderTime = object.getString("orderTime");
        String totalAmount = object.getString("totalAmount");
        String creater = object.getString("creater");
        String status = object.getString("status");

        String brand = object.getString("brand");
        String type = object.getString("type");
        String model = object.getString("model");
        String amount = object.getString("amount");
        String unit = object.getString("unit");
        String money = object.getString("money");
        String totalMoney = object.getString("totalMoney");
        x = x+1;
        String orderId = String.valueOf(x);
        int orderId1 =  Integer.parseInt(orderId);

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date myDate = null;
        try {
            myDate = sdf1.parse(orderTime);
        }catch (Exception e){
            e.printStackTrace();
        }

        Order order = new Order(orderNum,name,tel,myDate,totalAmount,creater,status);
        OrderDetail orderDetail = new OrderDetail(orderId1,orderNum,
                brand,type,model,amount,unit,money,totalMoney);

        boolean f1= orderService.addOrder(order);
        boolean f2= orderDetailService.addOrderDetail(orderDetail);

        if(f1&&f2){
            operate.setSuccess(true);
            operate.setMessage("订购单添加成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("订购单添加失败");
        }
        return operate;
    }
    @RequestMapping("/delOrder")
    @ResponseBody
    public LayUIOperate delOrder(HttpServletRequest request){
        LayUIOperate operate=new LayUIOperate();
        String orderNum = request.getParameter("orderNum");
        boolean f1= orderService.delOrder(orderNum);
        System.out.println(f1);
        if(f1){
            operate.setSuccess(true);
            operate.setMessage("订购单删除成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("订购单删除失败");
        }
        return operate;
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public LayUIOperate updateStatus(@RequestBody JSONObject object){
        LayUIOperate operate=new LayUIOperate();
        String orderNum = object.getString("orderNum");
        String status = object.getString("status");
        String opinion = object.getString("opinion");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        // 取得审核人
        String checker = "boss";
        Order order = new Order(orderNum,status,checker,opinion,date);
        boolean f1= orderService.updateStatus(order);
        System.out.println(f1);
        if(f1){
            operate.setSuccess(true);
            operate.setMessage("审核成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("审核失败");
        }
        return operate;
    }
}
